#include <iostream>
#include <mysql/mysql.h>
#include "stdDataBase.h"
#include "mysqlDataBase.h"
/* g++ main.cpp -lmysqlclient */

int main()
{
#if 0
    MYSQL * conn = NULL;
    const char * host = "localhost";
    const char * user = "root";
    const char * passwd = "1";

    StdDataBase * dataBase = new mysqlDataBase(host, user, passwd);

    const char * db = "musicDB";
    /* 创建数据库 */
    dataBase->connectDB(db);

    // const char * sql = "INSERT INTO userInfo (username, age) VALUES('yang', 22); ";
    // /* 执行数据库 */
    // dataBase->executeSql(sql);

    const char * sql = "select * from userInfo";
    /* 查询数据库 */
    dataBase->querySql(sql);

    /* 关闭数据库 */
    dataBase->close();
#endif


#if 0
    /* 初始化数据库句柄成功 */
    MYSQL * conn = mysql_init(NULL);
    if(conn == NULL)
    {
        std::cout << "mysql init failed" << std::endl;
        exit(-1);
    }

    const char * host = "localhost";
    const char * user = "root";
    const char * passwd = "1";  //测试root的密码，mysql -u root -p
    const char * db = "musicDB";
    unsigned int port = 3306;
    if(mysql_real_connect(conn, host, user, passwd, db , port, NULL, 0) == NULL)
    {
        std::cout << "mysql connect error." << std::endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    std::cout << "mysql connect success." << std::endl;
#if 0
    /* 插入数据 */
    const char * sql = "INSERT INTO userInfo (username, age) VALUES('shuaige', 20); ";

    if(mysql_query(conn, sql))
    {
        std::cout << "mysql query error" << std::endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
#endif

    /* 查询数据 */
    const char * sql = "select * from userInfo";

    if(mysql_query(conn, sql))
    {
        std::cout << "mysql query error" << std::endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    /* 从结果集里面拿取数据 */
    MYSQL_RES * res = mysql_store_result(conn);
    if(res == NULL)
    {
        std::cout << "store_result error" << mysql_error(conn) << std::endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }

    /* 获取列数 */
    unsigned int columns = mysql_num_fields(res);
    std::cout << "columns:" << columns << std::endl;

    /* 获取字段的名称 */
    MYSQL_FIELD * fields = mysql_fetch_fields(res);

    /* 打印所有列名 */
    for(int idx = 0; idx < columns; idx++)
    {
       std::cout << " " << fields[idx].name << "\t";
    }
    std::cout << std::endl;

    /* 获取每一行的数据 */
    MYSQL_ROW row;
    // 这里就是一层外循环了
    while((row = mysql_fetch_row(res)) != NULL)
    {
        for(int idx = 0; idx < columns; idx++)
        {
            const char * tmp = NULL;
            if(row[idx] != NULL)
            {
                tmp = row[idx];
            }
            else
            {
                tmp = "NULL";
            }
            std::cout << tmp << "\t";
        }
        std::cout << std::endl;
    }

    /* 关闭数据库 */
    mysql_close(conn);
#endif 


/* 测试mysql数据库事务 */
    MYSQL * conn = mysql_init(NULL);
    if(conn == NULL)
    {
        std::cout << "mysql init failed" << std::endl;
        exit(-1);
    }

    const char * host = "localhost";
    const char * user = "root";
    const char * passwd = "1";  //测试root的密码，mysql -u root -p
    const char * db = "musicDB";
    unsigned int port = 3306;
    if(mysql_real_connect(conn, host, user, passwd, db , port, NULL, 0) == NULL)
    {
        std::cout << "mysql connect error." << std::endl;
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    std::cout << "mysql connect success." << std::endl;

    /* 开启事务 */
    if(mysql_query(conn, "START TRANSACTION;"))
    {
        std::cout << "START TRANSACTION failed." << std::endl;
    }

    const char * sql1 = "update accont set money = money - 100 where usrname = 'zhangsan';";
    const char * sql2 = "drop table accont where usrname = 'test';";

    if(mysql_query(conn, sql1))
    {
        std::cout << "mysql1 execute error" << std::endl;
        /* 执行回滚 */
        mysql_query(conn, "ROLLBACK");
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    cout << "sql1 execute success" << endl;

    if(mysql_query(conn, sql2))
    {
        std::cout << "mysql1 execute error" << std::endl;
        /* 执行回滚 */
        mysql_query(conn, "ROLLBACK");
        /* 关闭数据库 */
        mysql_close(conn);
        exit(-1);
    }
    cout << "sql2 execute success" << endl;

    /* 提交事务 */
    mysql_query(conn, "COMMIT;");


    return 0;
}